package kr.hkit.summary_java.serviceui;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summary_java.dao.DaoInterface;
import kr.hkit.summary_java.erp.dto.CodeTitle;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.SQLException;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Panel_CodeTitleUITest extends JPanel implements ActionListener {

	private JButton btnDy;
	private String btnText;

	private DaoInterface<CodeTitle> dao;
	private InputPanel TitleNo;
	private InputPanel TitleName;

	private JPanel MainPanel;
	private JPanel BtnPanel;

	/**
	 * Create the panel.
	 */
	public Panel_CodeTitleUITest(String text, DaoInterface<CodeTitle> dao) {
		super();
		this.btnText = text;
		this.dao = dao;
		initialize();
	}

	private void initialize() {
		setLayout(new BorderLayout(0, 0));

		MainPanel = new JPanel();
		add(MainPanel, BorderLayout.CENTER);
		MainPanel.setLayout(new GridLayout(0, 1, 0, 0));

		TitleNo = new InputPanel("직책번호");
		MainPanel.add(TitleNo);
		TitleNo.setLayout(new GridLayout(1, 0, 0, 0));
		TitleNo.getTf().addFocusListener(new FocusAdapter() {

			@Override
			public void focusLost(FocusEvent e) {
				super.focusLost(e);

				int no = Integer.parseInt(TitleNo.getTf().getText().trim());
				CodeTitle c = new CodeTitle();
				c.setTitleNo(no);

				try {
					c = dao.searchItem(c);
					TitleName.setTfTxt(c.getTitleName());
					if (!btnDy.getText().equals("검색")) {
						panelEnable();
					}
				} catch (SQLException e1) {
					e1.printStackTrace();
				} catch (NullPointerException e1) {
					if (!btnDy.getText().equals("추가")) {
						JOptionPane.showMessageDialog(
								Panel_CodeTitleUITest.this, "해당직책이 없습니다.");

					} else {
						JOptionPane.showMessageDialog(
								Panel_CodeTitleUITest.this, "추가 가능");
					}
				}
			}

		});

		TitleName = new InputPanel("직책명");
		MainPanel.add(TitleName);

		BtnPanel = new JPanel();
		add(BtnPanel, BorderLayout.SOUTH);

		btnDy = new JButton(btnText);

		btnDy.addActionListener(this);
		BtnPanel.add(btnDy);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("추가")) {
			actionPerformedBtnAdd(e);
		} else if (e.getActionCommand().equals("삭제")) {
			actionPerformedBtnDel(e);
		} else if (e.getActionCommand().equals("수정")) {
			actionPerformedBtnUpdate(e);
		} else if (e.getActionCommand().equals("검색")) {
			actionPerformedBtnSearch(e);
		}
	}

	private void actionPerformedBtnSearch(ActionEvent e) {
		// 검색하기 위해서 titleno textfield에 입력한 값을 int형으로 가져와서 변수no에 담는다.
		int no = Integer.parseInt(TitleNo.getTf().getText().trim());
		CodeTitle c = new CodeTitle();
		c.setTitleNo(no); // int형으로 가져온 no를 c의 titleNo에 넣는다.
		try {
			c = dao.searchItem(c);
			TitleName.setTfTxt(c.getTitleName()); // c.tltlename을 가져와서
													// textfield에
													// 넣어준다.
			panelEnable(); // 검색이 되면 검색하기 위해서 입력한 titleNo는 비활성화 하고 나머지 Name은 활성화
							// 시켜준다
		} catch (SQLException e1) {
			e1.printStackTrace();
		} catch (NullPointerException e1) {
			JOptionPane.showMessageDialog(this, "존재하지 않음");
			TitleNo.setTfTxt("");// 존재하지 않는 값을 출력했으므로 textfield를 초기화 시켜주기 위해
									// 입력된다.

		}
	}

	private void actionPerformedBtnUpdate(ActionEvent e) {
		CodeTitle c = getCode();
		try {
			dao.updateItemByNo(c);
			JOptionPane.showMessageDialog(this, "수정되었습니다");
			panelDisable();
			panelClear();
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "수정실패 다시 확인하세요.");
		}

	}

	private void actionPerformedBtnDel(ActionEvent e) {
		CodeTitle c = getCode();

		try {
			dao.deleteItemByNo(c);
			JOptionPane.showMessageDialog(this, "삭제되었습니다");
			panelDisable();
			panelClear();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

	}

	protected void actionPerformedBtnAdd(ActionEvent e) {// 추가 버튼에 대한 Action
		CodeTitle c = getCode(); // getcode()는 textfield에 있는 값을 받아서 가져오는 역할
		try {
			dao.insertItem(c);
			JOptionPane.showMessageDialog(this, "입력되었습니다");
			panelClear();
		} catch (SQLException e1) {
			e1.printStackTrace();
			JOptionPane.showMessageDialog(this, "입력실패 다시 확인하세요.");
		}

	}

	private CodeTitle getCode() {
		int titleno = Integer.parseInt(TitleNo.getTf().getText().trim());
		String name = TitleName.getTf().getText();
		return new CodeTitle(titleno, name);
	}

	private void panelEnable() {
		TitleNo.getTf().setEnabled(false);
		TitleName.getTf().setEnabled(true);
	}

	private void panelDisable() {
		TitleNo.getTf().setEnabled(true);
		TitleName.getTf().setEnabled(false);
	}

	private void panelClear() {
		TitleNo.setTfTxt("");
		TitleName.setTfTxt("");
	}
}
